#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'fox16'
include Fox

require_relative 'irv_test_gen'

class IrvTestGenWindow < FXMainWindow

  def initialize(app)
    # Invoke base class initialize first
    super(app, "IrvTestGen", :opts => DECOR_ALL, :width => 300, :height => 230, :padLeft => 0, :padRight => 0)

    @source_xls = 'irv.xls'

    frame = FXVerticalFrame.new(self,
      FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y)

    matrix = FXMatrix.new(frame, 2,
      MATRIX_BY_COLUMNS|LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y)

    # Class
    FXLabel.new(matrix, "&Class:", nil,
      LAYOUT_CENTER_Y|JUSTIFY_RIGHT|LAYOUT_FILL_ROW)
    @classCombo = FXComboBox.new(matrix, 15, nil, 0,
      COMBOBOX_INSERT_LAST|COMBOBOX_STATIC|FRAME_SUNKEN|FRAME_THICK)
    @classCombo.connect(SEL_COMMAND, method(:onClassChanged))
    classes = getSheetsName(@source_xls)
    classes.each {|item| @classCombo.appendItem(item)}

    # From
    FXLabel.new(matrix, "&From:", nil,
      LAYOUT_CENTER_Y|JUSTIFY_RIGHT|LAYOUT_FILL_ROW)
    @fromCombo = FXComboBox.new(matrix, 15, nil, 0,
      COMBOBOX_INSERT_LAST|COMBOBOX_STATIC|FRAME_SUNKEN|FRAME_THICK)

    # To
    FXLabel.new(matrix, "&To:", nil,
      LAYOUT_CENTER_Y|JUSTIFY_RIGHT|LAYOUT_FILL_ROW)
    @toCombo = FXComboBox.new(matrix, 15, nil, 0,
      COMBOBOX_INSERT_LAST|COMBOBOX_STATIC|FRAME_SUNKEN|FRAME_THICK)

    updateVerbList(@classCombo.getItemText(@classCombo.currentItem))

    # Nb Lines
    FXLabel.new(matrix, "&Nb lines:", nil,
      LAYOUT_CENTER_Y|JUSTIFY_RIGHT|LAYOUT_FILL_ROW)
    @nbLinesSpinner = FXSpinner.new(matrix, 15, nil, 0,
      SPIN_NORMAL|FRAME_SUNKEN|FRAME_THICK|LAYOUT_SIDE_TOP)
    @nbLinesSpinner.value = 10
    @nbLinesSpinner.range = 1..1000

    # Solutions
    @solutionsCheck = FXCheckButton.new(frame,
      "Include solutions\tAdd solution below the test.")
    
    # Save path
    matrix = FXMatrix.new(frame, 3,
      MATRIX_BY_COLUMNS|LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y)
    FXLabel.new(matrix, "&Save path:", nil,
      LAYOUT_CENTER_Y|JUSTIFY_RIGHT|LAYOUT_FILL_ROW)
    @savepath = FXTextField.new(matrix, 25, nil, 0, (FRAME_SUNKEN|FRAME_THICK|
      LAYOUT_FILL_X|LAYOUT_CENTER_Y|LAYOUT_FILL_COLUMN))
    @savepath.text = "test.xls"
    saveBtn = FXButton.new(matrix,
      "...",
      :opts => FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_TOP|LAYOUT_LEFT,
      :padLeft => 10, :padRight => 10, :padTop => 5, :padBottom => 5)
    saveBtn.connect(SEL_COMMAND, method(:onSavePathButtonClicked))

    # Ok
    okBtn = FXButton.new(frame,
      "Ok",
      :opts => FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_FILL_COLUMN,
      :padLeft => 10, :padRight => 10, :padTop => 5, :padBottom => 5)
    okBtn.connect(SEL_COMMAND, method(:onGenerateTestButton))
  end

  # Called when the save path selection button is hit
  def onSavePathButtonClicked(sender, sel, ptr)
    saveDialog = FXFileDialog.new(self, "Save path selection")
    saveDialog.filename = @savepath.text
    if saveDialog.execute != 0 then
      @savepath.text = saveDialog.filename
    end
  end

  # Called when the class selection changed
  def onClassChanged(sender, sel, ptr)
    updateVerbList(ptr)
  end

  # Called when the user validate. Generate the test with the current parameters
  def onGenerateTestButton(sender, sel, ptr)
    # Get parameters values
    test_class = @classCombo.getItemText(@classCombo.currentItem)
    test_from = @fromCombo.getItemText(@fromCombo.currentItem)
    test_to = @toCombo.getItemText(@toCombo.currentItem)
    test_lines = @nbLinesSpinner.value
    test_savepath = @savepath.text
    
    # Generate the test
    test, solutions = generateTest(getVerbs(@source_xls, test_class), test_from, test_to, test_lines)
    solutions = nil if not @solutionsCheck.checked?

    # Export it in xls format at the given path
    exportTestToXls(test, solutions, test_savepath)
  end
  
  def create
    super
    show(PLACEMENT_SCREEN)
  end

  # Update the verb list in the from and to combobox depending on the given class
  def updateVerbList(className)
    verbs = getVerbs(@source_xls, className)
    verbs = Hash[verbs.sort]
    @fromCombo.clearItems
    @toCombo.clearItems
    verbs.each do |k, v| 
      @fromCombo.appendItem(k)
      @toCombo.appendItem(k)
    end
  end
end

if __FILE__ == $0
  # Make application
  application = FXApp.new("IrvTestGen", "Shosho")

  # Make window
  window = IrvTestGenWindow.new(application)

  # Create it
  application.create

  # Run
  application.run
end
